package com.ledim.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.adapter.GoneRoomsAdapter;
import com.ledim.bean.ChatRoomBean;
import com.ledim.db.LedimDBUtils;
import com.ledim.fragment.CommonDialogFragment;
import com.ledim.itf.LedimDialogClickListner;
import com.ledim.itf.LedimOnItemClickListener;
import com.ledim.utils.ConstantUtil;
import com.letv.android.young.client.R;
import java.util.List;

/**
 * 去过的放映厅
 */
public class GoneRoomsActivity extends LeDimBaseActivity
        implements LedimOnItemClickListener<ChatRoomBean> {

    private ImageView mBack;
    private TextView mSelectAll;
    private TextView mDelete;
    private TextView mEditButton;
    private ListView mListView;
    private View mDeleteLayout;
    private GoneRoomsAdapter mRoomsAdapter;
    private View mNoRoomsLayout;
    private CommonDialogFragment mClearGoneRoomDialog;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_gone_rooms;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gone_rooms);
        initView();
    }

    private void initView() {
        mBack = (ImageView) findViewById(R.id.gone_rooms_back);
        mEditButton = (TextView) findViewById(R.id.gone_rooms_edit);
        mSelectAll = (TextView) findViewById(R.id.gone_rooms_select_all);
        mDelete = (TextView) findViewById(R.id.gone_rooms_delete);
        mListView = (ListView) findViewById(R.id.listView_rooms);
        mDeleteLayout = findViewById(R.id.edit_button_layout);
        mNoRoomsLayout = findViewById(R.id.no_gone_rooms_layout);

        mBack.setOnClickListener(this);
        mEditButton.setOnClickListener(this);
        mSelectAll.setOnClickListener(this);
        mDelete.setOnClickListener(this);
        mDeleteLayout.setVisibility(View.GONE);
        mNoRoomsLayout.setVisibility(View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        showAllGoneRooms();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gone_rooms_back:
                finish();
                break;
            case R.id.gone_rooms_edit:
                if (mRoomsAdapter != null && mRoomsAdapter.getCount() > 0) {
                    if (mEditButton.getText().toString().trim().equals("编辑")) {
                        mEditButton.setText("取消");
                        mSelectAll.setText("全部选择");
                        mDelete.setText("删除");
                        mDelete.setBackgroundResource(R.drawable.rect_round_start_play_bg);
                        mDeleteLayout.setVisibility(View.VISIBLE);
                        mRoomsAdapter.mSelectedType = true;
                        mRoomsAdapter.notifyDataSetChanged();

                    } else if (mEditButton.getText().toString().trim().equals("取消")) {
                        mEditButton.setText("编辑");
                        mRoomsAdapter.getSelectedCards().clear();
                        mDeleteLayout.setVisibility(View.GONE);
                        mRoomsAdapter.mSelectedType = false;
                        mRoomsAdapter.notifyDataSetChanged();
                    }
                }
                break;
            case R.id.gone_rooms_select_all:
                if (mSelectAll.getText().equals("全部选择")) {
                    mRoomsAdapter.selectAll();
                    mDelete.setText("删除（" + mRoomsAdapter.getSelectedCount() + "）");
                    mDelete.setBackgroundResource(R.drawable.rect_round_delete_play_list_bg);
                    mSelectAll.setText("取消全部");
                } else if (mSelectAll.getText().equals("取消全部")) {
                    mRoomsAdapter.getSelectedCards().clear();
                    mRoomsAdapter.notifyDataSetChanged();
                    mDelete.setText("删除");
                    mDelete.setBackgroundResource(R.drawable.rect_round_start_play_bg);
                    mSelectAll.setText("全部选择");
                }
                break;
            case R.id.gone_rooms_delete:
                if (mRoomsAdapter.mSelectedRooms != null &&
                        !mRoomsAdapter.mSelectedRooms.isEmpty()) {
                    showClearDialog();
                }
                break;
        }
    }

    /**
     * 二次确认清除缓存弹窗
     */
    private void showClearDialog() {

        if (mClearGoneRoomDialog == null) {
            mClearGoneRoomDialog =
                    CommonDialogFragment.getInstance(ConstantUtil.DialogConstant.DIALOG_HORIZONTAL_TYPE, "清除去过的放映厅：除秽之刃，斩！", "确定", "取消", new LedimDialogClickListner() {
                        @Override
                        public void onClickPositive(View view) {
                            LedimDBUtils.deleteAllChatRoom(mRoomsAdapter.getSelectedCards());
                            mRoomsAdapter.clearCards();
                            initViewByRoomsCount(mRoomsAdapter.getCount());
                            mDelete.setText("删除");
                            mDelete.setBackgroundResource(R.drawable.rect_round_start_play_bg);
                        }

                        @Override
                        public void onClickNegative(View view) {

                        }

                        @Override
                        public void onDismiss() {

                        }
                    });
        }
        mClearGoneRoomDialog.showDialog(getSupportFragmentManager());
    }

    private void initViewByRoomsCount(int cardCount) {
        if (cardCount == 0) {
            setNoDataLayout();
        } else {
            mNoRoomsLayout.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
        }
    }

    //显示所有去过的放映厅
    private void showAllGoneRooms() {
        try {
            List mRoomLists = LedimDBUtils.queryChatRooms();
            if (mRoomLists.size() > 0) {
                if (mRoomsAdapter == null) {
                    mRoomsAdapter = new GoneRoomsAdapter(this, this, mRoomLists, false);
                    mListView.setAdapter(mRoomsAdapter);
                } else {
                    mRoomsAdapter.setDatas(mRoomLists);
                }
            } else {
                setNoDataLayout();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void setNoDataLayout() {
        mListView.setVisibility(View.GONE);
        mNoRoomsLayout.setVisibility(View.VISIBLE);
        mDeleteLayout.setVisibility(View.GONE);
        mEditButton.setText("编辑");
    }

    @Override
    public void onItemClick(int position, ChatRoomBean item, View v) {
        if (mRoomsAdapter.getSelectedCount() > 0) {
            mDelete.setText("删除（" + mRoomsAdapter.getSelectedCount() + "）");
            mDelete.setBackgroundResource(R.drawable.rect_round_delete_play_list_bg);
            if (mRoomsAdapter.isSelectedAll()) {
                mSelectAll.setText("取消全部");
            } else {
                mSelectAll.setText("全部选择");
            }

        } else {
            mSelectAll.setText("全部选择");
            mDelete.setText("删除");
        }
    }
}
